namespace OasisCiq.CommonTypes
{
    using System;
    using System.Text.RegularExpressions;

    /// <summary>
    /// Normalized and Collapsed String
    /// </summary>
    public class CTString : XS.XSNormalizedString
    {
        private static readonly Regex NormalizeAndCollapseRE = new Regex(@"\s+");
        private static readonly Regex ValidationRE = new Regex(@"(^\s.*)|(.*\s$)|(\s{2,})");

        public CTString(string value)
            : base(value)
        {
            if (ValidationRE.IsMatch(value))
            {
                throw new ArgumentException();
            }
        }

        public static string NormalizeAndCollapse(string value)
        {
            return NormalizeAndCollapseRE.Replace(value, " ").Trim();
        }

        public static implicit operator CTString(string argument)
        {
            return new CTString(argument);
        }

        public static implicit operator string(CTString argument)
        {
            return argument.Value;
        }
    }
}